Added PathElement
[brmtron.git] / BrmTron Server / src / cz / brmlab / brmtron / server / BrmTronServerThread.java
blobeafa7c58552e54888a18755eead3d575c088cc5a
1 package cz.brmlab.brmtron.server;
3 import java.io.BufferedReader;
4 import java.io.DataOutputStream;
5 import java.io.InputStreamReader;
6 import java.net.Socket;
7 import java.util.Map;
8 import java.util.Scanner;
9 import java.util.Set;
11 public class BrmTronServerThread extends Thread {
13 private Socket sock;
14 private int id;
15 private Player player;
18 public BrmTronServerThread(Socket sock, int id){
19 super("BrmTronServerThread");
20 this.sock = sock;
21 this.id = id;
22 this.player = new Player();
25 @Override
26 public void run() {
27 String s;
28 double[] loc = new double[2];
29 try {
30 DataOutputStream out = new DataOutputStream(sock.getOutputStream());
31 BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
33 player.setActive(true);
35 while((s=in.readLine()) != null) {
36 if(s.equalsIgnoreCase("die")){
37 player.setActive(false);
39 if(s.equals("quit")) {
40 break;
42 Scanner sc = new Scanner(s);
43 for(int i=0;sc.hasNextDouble();i++) {
44 loc[i] = sc.nextDouble();
46 player.setCoords(loc);
47 BrmTronServer.updatePlayer(id, player);
48 out.writeBytes(parsePlayers(BrmTronServer.getPlayers()));
51 out.close();
52 in.close();
53 sock.close();
54 } catch(Exception e) {
55 System.err.println(e);
60 private String parsePlayers(Map<Integer, Player> players) {
61 String ret = new String();
62 Set<Integer> keys = players.keySet();
64 for(int i: keys) {
65 double[] coords = players.get(i).getCoords();
66 boolean active = players.get(i).isActive();
67 ret += "ID: " + i + ", Long: " + coords[0] + ", Lat: " + coords[1] + ", Active: " + active + "\n";
70 return ret;